#include "modifyGridAction.h"



namespace modifyGridAction
{
	gridData* gData;


	void setGridData(gridData* gd)
	{
		gData = gd;
	}


	//If the cell spacing check button has been activated, this function creates a little spacing 
	//between the cells of the grid. Otherwise, it removes that spacing
	void modifyCellSpacing()
	{
		//If the checkbutton is active
		if(!gData->cellSpacing)
		{
			//If a grid has been created
			if(gData->grid)
			{
				gData->grid->set_row_spacings(1);
				gData->grid->set_col_spacings(1);
				gData->cellSpacing = true;
			}
		}
		else
		{
			if(gData->grid)
			{
				gData->grid->set_row_spacings(0);
				gData->grid->set_col_spacings(0);
				gData->cellSpacing = false;
			}
		}
	}


	//Signal handler activated when a cell of the grid is clicked
	bool cellClicked(GdkEventButton* event, int x, int y, int activeButton)
	{
		//Get the current type of the cell
		int state = *(gData->pmap + x + y*gData->width);

		//Obtain the pixbuf of the image
		Gtk::EventBox* eb = *(gData->pcells + x + y*gData->width);

		//Depending on the type of cell that is selected in the radio button frame
		if(activeButton == Pfv::ROAD_CELL)
		{
			//Get the current type of the cell
			state = *(gData->pmap + x + y*gData->width);

			//Check if the cell was the start or the goal
			if(state == Pfv::START_CELL)
			{ 
				//Unset the start coordinates
				gData->startX = -1;
				gData->startY = -1;
			}
			if(state == Pfv::GOAL_CELL)
			{
				//Unset the goal coordinates
				gData->goalX = -1;
				gData->goalY = -1;
			}

			//Modify the picture of this cell
			eb->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->ROAD_IMAGE);

			//Update the cell array
			*(gData->pmap + x + y*gData->width)  = Pfv::ROAD_CELL;
		}
		else if(activeButton == Pfv::GRASS_CELL)
		{
			//Check if the cell was the start or the goal
			if(state == Pfv::START_CELL)
			{ 
				//Unset the start coordinates
				gData->startX = -1;
				gData->startY = -1;
			}
			if(state == Pfv::GOAL_CELL)
			{
				//Unset the goal coordinates
				gData->goalX = -1;
				gData->goalY = -1;
			}

			//Modify the picture of this cell
			eb->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->GRASS_IMAGE);

			//Update the cell array
			*(gData->pmap + x + y*gData->width)  = Pfv::GRASS_CELL;
		}
		else if(activeButton == Pfv::TRAIL_CELL)
		{
			//Check if the cell was the start or the goal
			if(state == Pfv::START_CELL)
			{ 
				//Unset the start coordinates
				gData->startX = -1;
				gData->startY = -1;
			}
			if(state == Pfv::GOAL_CELL)
			{
				//Unset the goal coordinates
				gData->goalX = -1;
				gData->goalY = -1;
			}

			//Modify the picture of this cell
			eb->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->TRAIL_IMAGE);

			//Update the cell array
			*(gData->pmap + x + y*gData->width)  = Pfv::TRAIL_CELL;
		}
		else if(activeButton == Pfv::WALL_CELL)
		{
			//Check if the cell was the start or the goal
			if(state == Pfv::START_CELL)
			{ 
				//Unset the start coordinates
				gData->startX = -1;
				gData->startY = -1;
			}
			if(state == Pfv::GOAL_CELL)
			{
				//Unset the goal coordinates
				gData->goalX = -1;
				gData->goalY = -1;
			}

			//Modify the picture of this cell
			eb->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->WALL_IMAGE);

			//Update the cell array
			*(gData->pmap + x + y*gData->width)  = Pfv::WALL_CELL;
		}
		else if(activeButton == Pfv::START_CELL)
		{
			//Check if the cell was the goal
			if(state == Pfv::GOAL_CELL)
			{
				//Unset the goal coordinates
				gData->goalX = -1;
				gData->goalY = -1;
			}

			//If the cell is not already the start
			if(state != Pfv::START_CELL)
			{
				//If there is already a start cell
				if(gData->startX != -1)
				{
					//Get the image and the pixbuf of the current start cell
					Gtk::EventBox* ebStart = *(gData->pcells + gData->startX + gData->startY*gData->width);
					ebStart->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->ROAD_IMAGE);

					//Update the cell array
					*(gData->pmap + gData->startX + gData->startY*gData->width) = Pfv::ROAD_CELL;
				}

				//Modify the picture of this cell
				eb->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->START_IMAGE);

				//Update the cell array
				*(gData->pmap + x + y*gData->width)  = Pfv::START_CELL;

				//Update the start coordinates
				gData->startX = x;
				gData->startY = y;
			}
		}
		else if(activeButton == Pfv::GOAL_CELL)
		{
			//Check if the cell was the start
			if(state == Pfv::START_CELL)
			{
				//Unset the start coordinates
				gData->startX = -1;
				gData->startY = -1;
			}

			//If the cell is the goal
			if(state != Pfv::GOAL_CELL)
			{
				//If there is already a goal cell
				if(gData->goalX != -1)
				{
					//Get the image and the pixbuf of the current goal cell
					Gtk::EventBox* ebGoal = *(gData->pcells + gData->goalX + gData->goalY*gData->width);
					ebGoal->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->ROAD_IMAGE);

					//Update the cell array
					*(gData->pmap + gData->goalX + gData->goalY*gData->width) = Pfv::ROAD_CELL;
				}

				//Modify the picture of this cell
				eb->modify_bg_pixmap(Gtk::STATE_NORMAL, gData->GOAL_IMAGE);

				//Update the cell array
				*(gData->pmap + x + y*gData->width)  = Pfv::GOAL_CELL;

				//Update the start coordinates
				gData->goalX = x;
				gData->goalY = y;
			}
		}
	}


	//Signal handler activated when the mouse is over a cell
	//If the user is pressing the Shift key, the function cellClicked is called
	void mouseOverCell(GdkEventCrossing* event, int x, int y, int activeButton)
	{
		if(event->state == GDK_CONTROL_MASK) cellClicked(NULL, x, y, activeButton);
	}


	//Resize the image when the eventbox has been resized
	void resizeGridImage(Gdk::Rectangle& r, Gtk::EventBox* eb, int x, int y)
	{
		if(*(gData->pcalls + y*gData->width + x))
		{
			/*Gtk::Image* image = dynamic_cast<Gtk::Image*>(eb->get_child());
			Glib::RefPtr<Gdk::Pixbuf> pixb = image->get_pixbuf();
			Glib::RefPtr<Gdk::Pixbuf> scaled = pixb->scale_simple(r.get_width(), r.get_height(), Gdk::INTERP_TILES);
			image->set(scaled);*/
			*(gData->pcalls + y*gData->width + x) = false;
		}
		else
		{
			*(gData->pcalls + y*gData->width + x) = true;
		}
	}

};